package manager.data;

import java.util.ArrayList;

import manager.rawdata.LazyPersistedRawData;
import manager.rawdata.QueueBackupObject;


public class LazyPersistedQueue {

	private static ArrayList<RawDataObject> memoryQueue, memoryTmp;
	private static LazyPersistedRawData lprd;
	private EventsManager em;

	public LazyPersistedQueue(EventsManager em){
		memoryQueue = new ArrayList<RawDataObject>();
		memoryTmp = new ArrayList<RawDataObject>();
		em.setQueue(this);
		this.em=em;
		lprd = new LazyPersistedRawData(this);
		lprd.start();
	}

	public void enqueue(RawDataObject rdo) {
		push(rdo);
		pushTmp(rdo);
		synchronized(lprd){
			lprd.notify();
		}		
	}

	public void dequeue(){
		QueueBackupObject qbo = QueueBackupObject.getInstance(this);
		qbo.remove();
	}
	
	public void push(RawDataObject dpo){
		memoryQueue.add(dpo);
	}
	
	public void pushTmp(RawDataObject dpo){
		memoryTmp.add(dpo);
	}
	
	public RawDataObject pop(){
		if(memoryQueue.size()>0){
			RawDataObject rdo=memoryQueue.get(0);
			memoryQueue.remove(0);
			return rdo;
		}else
			return null;
	}
	
	public RawDataObject popTmp(){
		if(memoryTmp.size()>0){
			RawDataObject rdo=memoryTmp.get(0);
			memoryTmp.remove(0);
			return rdo;
		}else
			return null;
	}
	
	public int memorySize(){
		return memoryQueue.size();
	}
	
	public int memorySizeTmp(){
		return memoryTmp.size();
	}

	public void loadEvents() {
		em.processEvents();
	}
	
	
}